﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NF_Tools;
using NF_Base;
using NF_Web.Event;
using NF_Mode;
using System.Collections;
using Newtonsoft.Json;
using System.Runtime.InteropServices;
using NF_Bll;


namespace NF_Web
{
    public partial class FrmMain : FrmObject
    {

        //Online_AuditMap jiazhuokeji@
        public FrmMain()
        {
            InitializeComponent();

            //最大化
            this.Text = Title;
            this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
            this.WindowState = FormWindowState.Maximized;
        }




        /// <summary>
        /// LODE
        /// </summary>
        private void FrmMain_Load(object sender, EventArgs e)
        {
            MDISkin.MainForm_Shown(sender, e, this);

            #region - 设置MDI背景色 -

            MdiClient ctlMDI;
            foreach (Control ctl in this.Controls)
            {
                try
                {
                    ctlMDI = (MdiClient)ctl;
                    ctlMDI.BackColor = this.BackColor;
                }
                catch (InvalidCastException exc)
                {
                }
            }

            #endregion

            #region - 显示登陆用户 -

            lblBusName.Text = BaseClass.us.us_RealName;
            lblLogUser.Text = BaseClass.ro.ro_Name;

            #endregion

            #region - 获取可访问菜单 -
            
            BaseAnswer bar = new BaseAnswer();
            List<dynamic> lst = Menu_Bll<dynamic>.GetMenuByUserRoe(ref bar);
            if (bar.AnswerType != BaseAnswer.Answers.Succee)
            {
                MessageBoxShow.Alert(bar.AnswerMeg); return;
            }

            bool isSet = false;
            lst.ForEach(c => 
            {
               
                BF_TitleMenu tmu = new BF_TitleMenu();
                tmu.TitleText = c.mu_Name;
                tmu.BackColor = Color.FromArgb(225, 225, 225);
                if (!isSet)
                    tmu.IsFocus = isSet = true;
                else
                    tmu.IsFocus = false;

                tmu.Tag = c.mu_Id;
                tmu.DafuteImage = Image.FromFile(Application.StartupPath.Replace("bin\\Debug", "") + "Resources\\" + c.mu_Ico);
                tmu.LevelImage = Image.FromFile(Application.StartupPath.Replace("bin\\Debug", "") + "Resources\\" + c.mu_Ico);
                tmu.HoverImage = Image.FromFile(Application.StartupPath.Replace("bin\\Debug", "") + "Resources\\un" + c.mu_Ico);
                tmu.MenuClickEvent += mc_MenuClickEvent;
                this.flpMenu.Controls.Add(tmu);
            });

            #endregion


            
           
        }


        /// <summary>
        /// 菜单事件
        /// </summary>
        private void mc_MenuClickEvent(object sender, EventArgs e)
        {
            BF_TitleMenu mc = sender as BF_TitleMenu;

            BF_TitleMenu title = flpMenu.Controls.Cast<BF_TitleMenu>().SingleOrDefault(c => c.IsFocus);
            title.IsFocus = false;
            mc.IsFocus = true;
            


            OpenFrom(mc.Tag.ToString());
       
        }


        


        /// <summary>
        /// 关闭窗体
        /// </summary>
        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            //杀死进程
            System.Diagnostics.Process.GetCurrentProcess().Kill();
        }




        #region - 公告方法 -

        /// <summary>
        /// MDI加载子窗体
        /// </summary>
        private void ShowMDIFrom(Form frm)
        {
            Form[] MDIForm = this.MdiChildren;
            if (MDIForm.Length > 0)
                MDIForm.FirstOrDefault(c => { c.Close(); return false; });

            frm.MdiParent = this;
            frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            frm.Dock = DockStyle.Fill;
            frm.Show();
        }



        /// <summary>
        /// 打开菜单对应窗体
        /// </summary>
        /// <param name="index"></param>
        private void OpenFrom(string id)
        {
            Form frm = null;        //普通窗体
            Form frmDg = null;      //模态窗体

            switch (id)
            {
                case "96206B0E-145F-492A-8D4B-A5125E7B2B91": frm = new FrmWork(); break;//评分规则
                case "D38DFAE6-E5D2-476C-AF43-4AFCCEE4523C": frm = new FrmWork(); break;//用户管理
                case "A4A07F64-6C7C-4469-BCF5-CDEC5A8B9F4A": frm = new FrmWork(); break;//角色管理
                case "71D29661-0416-4CBB-9C2A-7E66DF006804": frm = new FrmWork(); break;//培训材料
                case "A437A72F-5920-45BE-896B-32877F97B37A": frm = new FrmWork(); break;//考务管理
                case "0C2759D7-6A97-4CD1-833F-CAAE71E46A96": frm = new FrmWork(); break;//权限分配
                case "CBE496BF-4D2D-4EB6-A20D-0AE73538EA9E": frm = new FrmWork(); break;//培训计划
                case "379DDE07-5867-42DA-8003-5D17E9714D13": frm = new FrmWork(); break;//考卷管理
                case "B5728682-C5E3-4806-BA21-0A2413420D5E": frm = new FrmWork(); break;//菜单管理
                case "5D1EFA81-BBF2-4B06-8944-689A8CED31B9": frmDg = new FrmWork(); break;//题库管理
                default: break;
            }
            if (frm != null)
                ShowMDIFrom(frm);
            if (frmDg != null)
                frmDg.ShowDialog();
        }

        #endregion






    }
}
